package com.example.waibao.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;

import java.util.Date;
import java.util.concurrent.*;

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

@Configuration
public class ScheduledThreadPoolServer {
    @Bean
    public ThreadPoolTaskExecutor getExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(5);
        executor.setMaxPoolSize(10);
        executor.setQueueCapacity(10);
        return executor;
    }
}



//@Configuration
//@EnableScheduling
//public class ScheduledThreadPoolServer {
//    //开启一个定时任务线程池
//    private static ScheduledExecutorService scheduler = new ScheduledThreadPoolExecutor(5);
//    //存放正在使用的定时任务
//    private static ConcurrentHashMap<String, Future> futureMap = new ConcurrentHashMap<>();
//    public Integer addTask(final String key,final String value){
////如果任务已经存在线程池，那么不重复添加
//        if(futureMap.contains(key)) return -1;
///**
// * initialDelay 表示初始化延迟
// * period 表示两次执行最小时间间隔
// */
//        Future future = scheduler.scheduleAtFixedRate(new Runnable() {
//            @Override
//            public void run() {
////执行具体的任务功能
//                System.out.println("任务: "+key+" 线程id: "+Thread.currentThread().getName()+" 时间start:"+new Date());
//            }
//        },0,5, TimeUnit.SECONDS);
////任务放入Map
//        if(!futureMap.contains(key)) futureMap.put(key,future);
//        return 1;
//    }
//
//    public void removeTask(String key){
//        Future future = futureMap.get(key);
////直接取消线程池中的任务
//        future.cancel(true);
//        futureMap.remove(key);
//    }
//}
